package com.quectel.cms.controller.notice;

import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.core.mobilebanner.MobileBannerConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysConstants;
import com.quectel.core.module.notice.dto.NoticeDto;
import com.quectel.core.module.notice.service.NoticeService;
import com.quectel.core.module.notice.service.NoticeVillageService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-13 16:36:57
 */
@RestController
@RequestMapping("notice")
@Tag(name = "NoticeController", description = "小区公告相关api")
public class NoticeController extends BaseController {

    @DubboReference
    private NoticeService noticeService;
    @DubboReference
    private NoticeVillageService noticeVillageService;

    /**
     * 小区公告分页列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "notice:list")
    @Operation(summary = "小区公告分页列表")
    public Response<Page<NoticeDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = noticeService.queryTotal(request.getParams());

        List<NoticeDto> list = null;
        if (total > 0) {
            list = noticeService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<NoticeDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<NoticeDto>>ok().wrap(page);
    }


    /**
     * 查询小区公告单条记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "notice:info")
    @Operation(summary = "查询小区公告单条记录")
    public Response<NoticeDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (noticeService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        NoticeDto noticeDto = noticeService.selectById(id);

        if (MobileBannerConstants.BINDING_VILLAGE_PART.equals(noticeDto.getIsBindingAll())) {
            List<VillageDto> villageDtoList = noticeVillageService.selectByNoticeId(noticeDto.getId());
            noticeDto.setVillageDtoList(villageDtoList);
        }

        return Response.<NoticeDto>ok().wrap(noticeDto);
    }

    /**
     * 保存小区公告单条记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "notice:edit")
    @Operation(summary = "保存小区公告单条记录")
    public Response<Object> save(@RequestBody @Validated NoticeDto noticeDto) {

        //填充基础数据
        fillSaveCommonData(noticeDto);


        if (MobileBannerConstants.BINDING_VILLAGE_ALL.equals(noticeDto.getIsBindingAll())) {
            //绑定全部小区
            if (!SysConstants.SYS_TOP_GROUP_CODE.equals(SessionHolder.getSysUser().getGroupCode())) {
                //只有超级组才能
                return Response.error("您的权限不能设置关联所有小区,请关联部分小区");
            }
        }

        noticeService.saveOrUpdate(noticeDto);

        return Response.ok();
    }

    /**
     * 修改小区公告单条记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "notice:edit")
    @Operation(summary = "修改小区公告单条记录")
    public Response<Object> update(@RequestBody @Validated NoticeDto noticeDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(noticeDto.getId()));
        if (noticeService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(noticeDto);


        if (MobileBannerConstants.BINDING_VILLAGE_ALL.equals(noticeDto.getIsBindingAll())) {
            //绑定全部小区
            if (!SysConstants.SYS_TOP_GROUP_CODE.equals(SessionHolder.getSysUser().getGroupCode())) {
                //只有超级组才能
                return Response.error("您的权限不能设置关联所有小区,请关联部分小区");
            }
        }

        noticeService.saveOrUpdate(noticeDto);

        return Response.ok();
    }

    /**
     * 修改小区公告单条记录
     *
     * @param noticeDto
     * @return
     */
    @PostMapping("updateStatus")
    @RequirePermissions(values = "notice:edit")
    @Operation(summary = "修改小区公告单条记录")
    public Response<Object> updateStatus(@RequestBody NoticeDto noticeDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(noticeDto.getId()));
        if (noticeService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        //填充基础数据
        fillEditCommonData(noticeDto);

        noticeService.updateById(noticeDto);

        return Response.ok();
    }

    /**
     * 批量删除小区公告记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "notice:delete")
    @Operation(summary = "批量删除小区公告记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (noticeService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        noticeService.deleteBatch(ids);

        return Response.ok();
    }
}
